package compilador;
import java.io.Reader;
import java_cup.runtime.Symbol;
%%
%class Scanner
%unicode 
%cup
%function next_token
%type java_cup.runtime.Symbol
%line
%column
%{
    private TablasSimbolos tabla;
    public Scanner(Reader in, TablasSimbolos t){
    this(in);
    this.tabla = t;
    }
    int lineaActual = 1;
    private static int actualEtq=0;
    private static String nuevaEtq() {
    return "etqL"+(++actualEtq);
    }
%}

%{

    private Symbol symbol(int type){
    return new Symbol(type,yyline,yycolumn);
  }
// Crea un nuevo objeto java_cup.runtime.Symbol con información sobre el token actual con valor
  private Symbol symbol(int type,Object value){
    return new Symbol(type,yyline,yycolumn,value);
  }
%}
%eofval{
    return symbol(sym.EOF);
%eofval}
%eofclose

nuevalinea          = \r|\n|\r\n
digito              = [0-9]+ 
letra               = [a-zA-Z] 
palabra             = [a-zA-Z]+
identificador       = palabra | letra
string              = "'" [a-zA-Z]+ "'"
digitofloat         = [1-9]+"."digito+
symerror            = "?"|"!"|"@"|"$"|"%"|"^"|"*"|"+"|"~"|";"|"→"|"×"|"π"|"∪"|"−" 
white               = [ \t\f]+
fecha               = digito "/" letradigito "/" digito

%state STRING

%%
//reservados

"~E"    { return symbol (sym.EXIS);}
"~U"    { return symbol (sym.UNIV);}
"~P"    { return symbol (sym.PERT);}
"AND"   { return symbol (sym.LOGICO_Y);}
"OR"    { return symbol (sym.LOGICO_O);}
"NOT"   { return symbol (sym.NEGACION);}


{identificador} { 
Simbolo s;
if ((s = tabla.buscar(yytext())) == null)
s = tabla.insertar(yytext(),"-");
return symbol(sym.IDENTIFICADOR, s); 
}
{digitofloat}   { return symbol(sym.FLOAT_LITERAL, new Float(yytext()));}
{letra}         { return symbol(sym.LETRA, new String(yytext()));}
{palabra}       { return symbol(sym.PALABRA, new String(yytext()));}
{string}        { return symbol(sym.STRING, new String(yytext()));}



"="	{ return symbol(sym.IGUAL);}

"=="	{ return symbol(sym.IGUAL_IGUAL);}
"!="	{ return symbol(sym.DISTINTO);}
"<"	{ return symbol(sym.MENOR);}
">"	{ return symbol(sym.MAYOR);}
"<="	{ return symbol(sym.MENORIGUAL);}
">="	{ return symbol(sym.MAYORIGUAL);}

"&&"	{ return symbol(sym.AMPERSON);}
"|"	{ return symbol(sym.BARRA);}
"#"     { return symbol(sym.NUMERAL);}

"("	{ return symbol(sym.PARENTIZQ);}
")"	{ return symbol(sym.PARENTDER);}
"{"	{ return symbol(sym.LLAVEIZQ);}
"}"	{ return symbol(sym.LLAVEDER);}

","	{ return symbol(sym.COMA);}
"."	{ return symbol(sym.PUNTO);}
"["	{ return symbol(sym.CORIZQ);}
"]"	{ return symbol(sym.CORDER);}

"->"    { return symbol(sym.IMPLICA);}
"_"     { return symbol(sym.GUIONBAJO);}
"-"     { return symbol(sym.GUIONMEDIO);}

{white}             { /* Ignorar Espacios en Blanco. */ }
{nuevalinea}        { /* Ignorar nueva linea. */ }
{digito}            { return symbol(sym.ENTERO,new Integer(yytext())); }
{fecha}             { return symbol(sym.FECHA, new String(yytext()));}
{symerror}          { System.err.println("Caracter Ilegal: "+yytext()); }